package com.frxuexi.shop.view

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.frxuexi.adapter.ShopAdapter
import com.frxuexi.lib_base.entity.ExplosiveMoney
import com.frxuexi.lib_base.entity.ShopBean
import com.frxuexi.lib_base.entity.ShopHomeBean
import com.frxuexi.lib_base.mvi.BaseFragment
import com.frxuexi.lib_base.mvi.RequestType
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.shop.R
import com.frxuexi.shop.databinding.FragmentShopBinding
import com.frxuexi.shop.intent.ShopIntent
import com.frxuexi.shop.viewmodel.ShopViewModel
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@AndroidEntryPoint
class ShopFragment : BaseFragment<FragmentShopBinding,ShopIntent,ShopViewModel>() {
    override val binding: FragmentShopBinding by lazy { FragmentShopBinding.inflate(layoutInflater) }
    override val vm: ShopViewModel by viewModels<ShopViewModel>()
    var types = mutableListOf<ExplosiveMoney>()
    val shopAdapter by lazy { ShopAdapter() }
    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.DEFAULT->{
                                val homeBean = it.data as ShopHomeBean
                                binding.shopType.apply {
                                    types.addAll(homeBean.explosiveMoney)
                                    homeBean.explosiveMoney.forEach {
                                        addTab(newTab().setText(it.name))
                                    }
                                }
                            }
                            RequestType.SHOPS->{
                                val shopBean = it.data as ShopBean
                                Log.i("lr",shopBean.toString())
                                shopAdapter.apply {
                                    addData(shopBean.list)
                                    notifyDataSetChanged()
                                }
                            }

                            else -> {}
                        }

                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        vm.sendIntent(ShopIntent.getHomeBean)
        binding.shopRv.apply {
            adapter=shopAdapter
            layoutManager=StaggeredGridLayoutManager(2,RecyclerView.VERTICAL)
        }

        shopAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/shop/ShopDetailActivity").withString("sid",shopAdapter.data.get(position).id.toString()).navigation()
        }

        binding.shopType.addOnTabSelectedListener(object :OnTabSelectedListener{
            override fun onTabSelected(tab: TabLayout.Tab?) {
                val typeBean = types.get(tab?.position!!)
                vm.sendIntent(ShopIntent.getShops(typeBean.type.toInt(),1,10))
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

        })
    }

}